package com.egao.common.manage.school;

import cn.hutool.core.util.StrUtil;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.world.entity.WorldGameArticle;
import com.egao.common.module.world.service.WorldGameArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 济世杯文章管理
 *
 * @author lsj
 */
@Api(tags = "济世杯文章管理")
@RestController
@CrossOrigin
@RequestMapping("${api}")
public class SchoolWorldGameArticleController extends BaseController {
    @Autowired
    private WorldGameArticleService worldGameArticleService;

    @PreAuthorize("hasAuthority('school:world-game-article:list')")
    @ApiOperation("分页查询文章")
    @OperLog(value = "文章管理", desc = "分页查询文章")
    @GetMapping("/school/world-game-article/page")
    public PageResult<WorldGameArticle> page(HttpServletRequest request) {
        PageParam<WorldGameArticle> pageParam = new PageParam<>(request);
        pageParam.setDefaultOrder(null, new String[]{"wga.create_time"});

        if (!StrUtil.isBlank(getLoginUser().getCollegeCode())) {
            pageParam.put("createBy", getLoginUserId());
        }

        return new PageResult<>(worldGameArticleService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('school:world-game-article:save')")
    @ApiOperation("添加文章")
    @OperLog(value = "文章管理", desc = "添加文章", result = true)
    @PostMapping("/school/world-game-article/insert")
    public JsonResult insert(@RequestBody WorldGameArticle worldGameArticle) {
        worldGameArticle.setCreateBy(getLoginUserId());
        return worldGameArticleService.insertArticle(worldGameArticle);
    }

    @PreAuthorize("hasAuthority('school:world-game-article:update')")
    @ApiOperation("修改文章")
    @OperLog(value = "文章管理", desc = "修改文章", result = true)
    @PutMapping("/school/world-game-article/update")
    public JsonResult update(@RequestBody WorldGameArticle worldGameArticle) {
        return worldGameArticleService.updateArticle(worldGameArticle);
    }

    @PreAuthorize("hasAuthority('school:world-game-article:delete')")
    @ApiOperation("删除文章")
    @OperLog(value = "文章管理", desc = "删除文章", result = true)
    @DeleteMapping("/school/world-game-article/delete")
    public JsonResult delete(Integer id) {
        return worldGameArticleService.deleteArticle(id);
    }

    @PreAuthorize("hasAuthority('school:world-game-article:list')")
    @ApiOperation("查询单个文章")
    @OperLog(value = "文章管理", desc = "查询单个文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章id", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/school/world-game-article/get")
    public JsonResult listGetId(Integer id) {
        return worldGameArticleService.getId(id);
    }


}
